﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage;
using MusicPlayer.Data.DataModel;

namespace MusicPlayer.Data;

public sealed class PlayerContext : DbContext
{
    public DbSet<PlaylistDataModel> Playlists { get; set; }
    public DbSet<MusicDataModel> Musics { get; set; }
    public DbSet<HistoryDataModel> History { get; set; }

    private string DbPath => "music.db";

    public void Init()
    {
        Database.EnsureCreated();
    }

    protected override void OnConfiguring(DbContextOptionsBuilder options)
        => options.UseSqlite($"Data Source={DbPath}");

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<MusicDataModel>()
            .HasOne(x => x.Playlist)
            .WithMany(x => x.Musics);
    }
}